﻿using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.InterfaceService;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class SystemEnvService:BaseService,ISystemEnvService
    {
        [Autowired]
        private IFileHelper fileHelper { get; set; }

        [Autowired] 
        private SystemConfigEntity systemConfig { get; set; }
        [Autowired]
        private IConfiguration config { get; set; }
        public SystemEnvService()
        {

        }

        public async Task<BaseMessage> SetSystemEnv(int env)
        {

            #region verify params
            if (env != (int)eGroupGetDateType.Quarter && env != (int)eGroupGetDateType.Month)
            {
                return BadRequest400("环境变量的值设置错误,请重新尝试!");
            }
            #endregion
            string path = config["sysconfig"];
            fileHelper.SetFileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,path));
            systemConfig.QuarterOrMonth = env;
            if(fileHelper.UpdateFileContent(systemConfig))
            {
                return Ok200("设置成功!");
            }
            return BadRequest400("设置失败!");
        }
        public  BaseMessage GetEnvs()
        {
            List<PieView> pieList = new List<PieView>();
            pieList.Add(new PieView() { Name= "按月统计数据",Value=(int)eGroupGetDateType.Month });
            pieList.Add(new PieView() { Name= "按季度统计",Value=(int)eGroupGetDateType.Quarter });
            return Ok200("查询成功!",pieList);
        }
        public DataMessage GetSystemEnv()
        {
            List<PieView> pie=config.GetSection("StatisticsType").Get<List<PieView>>();
            PieView data = pie.Where(e=>e.Value== systemConfig.QuarterOrMonth).FirstOrDefault();
            return Ok200("查询成功!",data);
        }
    }
}
